from flask_restful import abort
from sqlalchemy.orm import validates

from comm.sqlalchemyConfig import db
from model.base import BaseModel

class xsjBookModel(BaseModel):
    """
    用户表
    """
    __tablename__ = 'xsj_book'
    id = db.Column(db.String(125), primary_key=True, autoincrement=False,doc='id')
    name = db.Column(db.String(125), nullable=False,unique=True,doc='用户名')
    is_del = db.Column(db.Boolean, nullable=True,default=False,doc='是否删除')
    atm = db.Column(db.Numeric(20,6), nullable=True,default=0,doc='金额')
    author = db.Column(db.INTEGER,db.ForeignKey('xsj_author.id'),doc='作者')
    rem = db.Column(db.Text, nullable=True,default=None,doc='备注')

    @validates('name')
    def validate_address(self, key, name):
        existData = self.__class__.query.filter_by(name=name).first()
        if existData != None:
            abort(400, msg='用户名重复')
        return name

    def __str__(self):
        return 'xsj_book:{}'.format(self.name)